define(['jquery', 'jquery-validation','jquery-validation/localization/messages_zh'], function($) {
    //包含特殊字符
    $.validator.addMethod('ltHundred',function (value,element) {
        value = $.trim(value);
        var numericRegex = /^[0-9]+$/;
        var flag = 1*value <= 100&&1*value >0;
        return  numericRegex.test(value)&&flag;
    },"该字段必须为0-100的整数");
    $.validator.setDefaults({
        ignore: 'input[type=hidden]',
        highlight: function(element) {
            $(element).addClass('has-error')
        },
        unhighlight: function(element) {
            $(element).removeClass('has-error');
        },
        errorElement: 'span',
        errorClass: 'error',
        errorPlacement: function(error, element) {
            var $target = $('#error-place');
            $target.html(error);
            //$("<span id='one-error' class='error'>请按要求填写</span>").appendTo($target);
        }
    });
    return {
        validator: function(opt,rule,msg,bundle,callback) {
            var $thisForm = $(opt.form);
            var $btn = $(opt.button);
            var url = $thisForm.attr('action');
            $thisForm.validate({
                rules:rule,
                messages:msg,
                submitHandler: function(form) {
                    // 已经通过了验证
                    var data = bundle();
                    if(data == false){
                        return false;
                    }
                    $.ajax(url, {
                            method: 'POST',
                            contentType: "application/json; charset=utf-8",
                            data: JSON.stringify(data)
                        })
                            .then(function(response) {
                                callback(response);
                            });
                }
            });
        }
    };

});
